{extend name="platform/base" /}
{block name="resources"/}
<style>
.table{width:98%;display:table;border-left:1px dotted #e5e5e5;border-right:1px dotted #e5e5e5;border-bottom:1px dotted #e5e5e5;background:rgba(204,204,204,.09);padding:0 10px 10px 10px}
.table-body{min-height:50px;max-height:410px;overflow-y:auto;overflow-x:hidden}
.inline-block{display:inline-block}
.w5{width:5%}
.w75{width:70%}
.w19{width:10%;text-align:center}
.w100{width:100%;height:40px;line-height:40px;border-bottom:1px dotted #e5e5e5}
.add-spec{height:40px;line-height:40px;border-bottom:1px dotted #E6E6E6}
input{vertical-align:initial}
.table input[type=text],input.password,input.text,input[type=password]{font:12px/20px Arial;color:#777;background-color:#FFF;vertical-align:baseline;margin-bottom:0}
.set-style dl dt{text-align:left;width:7%}
label.line-label{display:inline-block;margin-right:10px;font-weight:400}
.edit_button{background-color:#53b567;border-color:#4bae5f;color:#fff;width:80px;line-height:30px;border:0}
.textarea-common{border-radius:0!important;resize:none!important;width:250px!important;box-sizing:border-box!important}
.h100{height:100px;}
</style>
{/block}
{block name="main"}
<section class="panel panel-default">
	<header class="panel-heading font-bold"> 添加规格 </header>
	<div class="panel-body">
		<div class="form-horizontal" id="goods_brand_form" name="form">
			<div class="form-group">
				<label class="col-sm-2 control-label"><span class="color-red">*</span>规格名称</label>
				<div class="col-md-2">
					<input type="text" class="form-control" id="spec_name" name="spec_name" value="" required onkeydown="error_hide()">
				</div>
			<p class="error">规格名称不能为空</p>
			</div>
            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
				<label class="col-sm-2 control-label">规格排序</label>
				<div class="col-md-1">
					<input type="text" class="form-control" id="sort" value="255" onkeyup='this.value=this.value.replace(/\D/gi,"")' required>
				</div>
<!-- 				<label class="col-md-9 error" for="brand_initial" ></label> -->
			</div>
            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
				<label class="col-sm-2 control-label">是否启用</label>
				<div class="col-md-4">
					<label class="switch">
                         <input id="is_visible" type="checkbox" checked>
                         <span></span> 
                    </label>
				</div>
			</div>
			
			<div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
				<label class="col-sm-2 control-label">是否筛选</label>
				<div class="col-md-4">
					<label class="switch">
                         <input id="is_screen" type="checkbox" checked>
                         <span></span> 
                    </label>
				</div>
			</div>
			
			<div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
				<label class="col-sm-2 control-label">规格值：</label>
				<div class="col-md-4">
					
                         <textarea rows="" cols="" id="spec_value" class="textarea-common h100"></textarea>
						 <p class="hint" style="color: #BBB;margin-top: 10px;">一行为一个规格项，多个规格项用换行输入</p>
               
				</div>
			</div>
			
			<div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
				<label class="col-sm-2 control-label">规格说明：</label>
				<div class="col-md-4">
                         <textarea rows="" cols="" id="spec_des" class="textarea-common"></textarea>
				</div>
			</div>
            <!-- <div class="line line-dashed line-lg pull-in"></div>
            <div class="table">
				<div class="w100">
					<div class="col-sm-2 control-label"></div>
					<div class="inline-block w75">属性名称</div>
					<div class="inline-block w19">操作</div>
				</div>
				<div class="table-body">
				<div class="w100 spec_data">
					<div class="col-sm-2 control-label"><label style="margin-left: 5px;"> <input type="checkbox" style=""></label></div>
					<div class="inline-block w75">
						<input type="text" name="spec_value" style="margin-right: 20px;">
					</div>
					<div class="inline-block w19"><a href="javascript:;" onclick="delSpecValue(this)">删除</a></div>
				</div>
				</div>
				<div><div class="col-sm-2 control-label"></div><a href="javascript:;" onclick="addSpecValue(this)"><i class="fa fa-plus"></i>&nbsp;&nbsp;添加一个属性</a></div>
			</div> -->
            
            <div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<div class="col-sm-4 col-sm-offset-2">
					<button class="edit_button" onclick="addGoodsSpec();">提交</button>
<!-- 					<a href="javascript:void(0);" onclick="addGoodsBrandAjax()" class="btn btn-primary">保存</a> -->
				</div>
			</div>
		</div>
	</div>
</section>
<script>
var flag = false;




function addGoodsSpec() {
	var spec_name = $.trim($("#spec_name").val());
	var sort = $("#sort").val();
	var show_type = 1
	var spec_des = $('#spec_des').val();
	if ($("#is_visible").prop("checked")) {
		var is_visible = 1;
	} else {
		var is_visible = 0;
	}
	if ($("#is_screen").prop("checked")) {
		var is_screen = 1;
	} else {
		var is_screen = 0;
	}
	
	var spec_value_str = $('#spec_value').val();
	var reg = new RegExp("\n","g");//g,表示全部替换。
	var space = new RegExp(" ","g");
	spec_value_str = spec_value_str.replace(reg, ",");
	
	if(spec_name == ''){
		$("#spec_name").focus();
		showTip("规格名称不能为空",'warning');
		return false;
	}

	spec_value_arr = spec_value_str.split(',');
	for(i in spec_value_arr){
		if(spec_value_arr[i].length > 0){
			if(!vertifyStr(spec_value_arr[i])){
				showTip("规格值不能包含特殊字符",'warning');
				return false;
			}
		}
	}

	spec_value_str = spec_value_str.replace(space, "&nbsp;");

	if(spec_value_str == ''){
		$('#spec_value').focus();
		showTip("规格值不能为空！",'warning');
		return false;
	}
	
	if(flag) return;
	flag = true;
	$.ajax({
		type : "post",
		url : "{:__URL('PLATFORM_MAIN/goods/addgoodsspec')}",
		data : {
			'spec_name' : spec_name,
			'sort' : sort,
			'is_visible' : is_visible,
			'show_type' : show_type,
			'spec_value_str' : spec_value_str,
			'is_screen':is_screen,
			'spec_des' : spec_des
		},
		success : function(data) {
			if (data["code"] > 0) {
				showTip("添加成功","success");
				location.href = "{:__URL('PLATFORM_MAIN/goods/goodsspeclist')}";
			} else {
				showTip("添加失败","error");
				flag = false;
			}
		}
	});
}



//验证字符串不能含有特殊字符
function vertifyStr(str){
	var regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]\\]/im,
 regCn = /[·！#￥（——）：；“”‘、，|《。》？、【】[\]]/im;

	if(regEn.test(str) || regCn.test(str)) {
	    return false;
	}else{
		return true;
	}
}

</script>
{/block}